﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Arrays Exercise 2</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Compares two char arrays lexicographically (letter by letter).</h1>
    <label for="arr1-input">Array 1:</label>
    <input type="text" id="arr1-input" />
    <label for="arr2-input">Array 2:</label>
    <input type="text" id="arr2-input" />
    <button onclick="execute()">Execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function execute()
        {
            var val1 = jsConsole.read("#arr1-input");
            var val2 = jsConsole.read("#arr2-input");
            var index = 0;
            var res = 0;

            // Campare every 2 letters until one or the array's letters finish
            while ((index < val1.length) && (index < val2.length))
            {
                if (val1[index] > val2[index])
                {
                    res = 1;
                    break;
                }
                if (val2[index] > val1[index])
                {
                    res = -1;
                    break;
                }
                index++;
            }

            // Check if strings are equal so far
            if (0 == res)
            {
                // Check is there are more letters at the end
                if (index < val1.length)
                {
                    res = 1;
                }
                else if (index < val2.length)
                {
                    res = -1;
                }
            }
            
            // Final result
            switch (res)
            {
                case 1:
                    jsConsole.writeLine("Array 1 > Array 2");
                    break;
                case -1:
                    jsConsole.writeLine("Array 2 > Array 1");
                    break;
                case 0:
                    jsConsole.writeLine("Array 1 == Array 2");
                    break;
            }
        }
    </script>
</body>
</html>
